/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database_client;

import Shared.NodeInfo;
import java.awt.Color;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 *
 * @author Backkom
 */
public class TreeRenderer extends DefaultTreeCellRenderer {

    ImageIcon imgDB;
    ImageIcon imgTB;
    ImageIcon imgRoot;
    ImageIcon imgKey;
    ImageIcon imgCol;

    public TreeRenderer() {
        imgDB = new ImageIcon(getClass().getResource("/database_client/resource/database_icon.png"));
        imgTB = new ImageIcon(getClass().getResource("/database_client/resource/table_icon.png"));
        imgRoot = new ImageIcon(getClass().getResource("/database_client/resource/root_icon.png"));
        imgKey = new ImageIcon(getClass().getResource("/database_client/resource/primary_key.png"));
        imgCol = new ImageIcon(getClass().getResource("/database_client/resource/column_icon.png"));
    }

    @Override
    public Component getTreeCellRendererComponent(
            JTree tree,
            Object value,
            boolean sel,
            boolean expanded,
            boolean leaf,
            int row,
            boolean hasFocus) {

        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
        String type = ((NodeInfo) node.getUserObject()).GetType();
        if (type.compareToIgnoreCase("root") == 0) {
            setIcon(imgRoot);
        }
        if (type.compareToIgnoreCase("db") == 0) {
            setIcon(imgDB);
        }
        if (type.compareToIgnoreCase("tb") == 0) {
            setIcon(imgTB);
        }
        if (type.compareToIgnoreCase("key") == 0) {
            setIcon(imgKey);
        }
        if (type.compareToIgnoreCase("cl") == 0) {
            setIcon(imgCol);
        }
        setBackgroundNonSelectionColor(tree.getBackground());
        if (sel) {
            setForeground(Color.WHITE);
        } else {
            setForeground(Color.BLACK);
        }
        return this;
    }
}
